# rules.mk - Rules for compiling and substituting.
# Copyright (C) 2007  Mikhail Vorozhtsov
# See the LICENSE section of the README file for details.

# $Id$

obj-files = \
  $(foreach f,$(1), \
     $(join $(if $(ARCH),$(if $(PLATFORM),$(ARCH)-$(PLATFORM),$(ARCH)).,), \
            $(subst /,--,$(patsubst sources/%,%,$(basename $(f)))).o))

my-rules = $(foreach pair,$(join $(2),$(addprefix @,$(3))), \
                     $(eval $(call $(1), \
                                     $(firstword $(subst @, ,$(pair))), \
                                     $(lastword $(subst @, ,$(pair))),$(4))))

define ASM_RULE
$(2): $(1)
	$$(CXX) \
	  $$($(3)ASFLAGS) $$(ASFLAGS_$$<) \
	  -DIN_ASM_CODE $$($(3)CPPFLAGS) $$($(3)ASCPPFLAGS) $$(CPPFLAGS_$$<) \
	  -MMD -MF $$(@:%.o=%.d) -c -o $$@ $$<
	$$(if $$(BFD_$$<),$$(OBJCOPY) -O $$(BFD_$$<) $$@ $$@)
endef

asm-rules = $(call my-rules,ASM_RULE,$(1),$(2),$(if $(3),$(3)_,))

define CXX_RULE
$(2): $(1)
	$$(CXX) \
	  $$($(3)ASFLAGS) $$(ASFLAGS_$$<) \
	  -DIN_CXX_CODE $$($(3)CPPFLAGS) $$($(3)CXXCPPFLAGS) $$(CPPFLAGS_$$<) \
	  $$($(3)CXXFLAGS) $$(CXXFLAGS_$<) \
	  -MMD -MF $$(@:%.o=%.d) -c -o $$@ $$<
	$$(if $$(BFD_$$<),$$(OBJCOPY) -O $$(BFD_$$<) $$@ $$@)
endef

cxx-rules = $(call my-rules,CXX_RULE,$(1),$(2),$(if $(3),$(3)_,))

define SED_RULE
$(2): $(1)
	test -e $(dir $(2)) || mkdir -p $(dir $(2))
	$(SED) \
	  $$(SEDFLAGS) $$(SEDFLAGS_$$<) \
	  $$< > $$@
endef

sed-rules = $(call my-rules,SED_RULE,$(1),$(2))

